import * as xml2js from 'xml2js';
/**
 * 解析xml
 */
export const parseXml = (str: string): Record<string, any> =>
  new Promise((resolve, reject) => {
    const parseString = xml2js.parseString;
    parseString(str, { explicitArray: false }, (err, json) => {
      if (json) {
        resolve(json.xml);
      } else {
        reject(err);
      }
    });
  });

/**
 * 创建xml
 */
export const createXml = (obj) => {
  const builder = new xml2js.Builder({
    rootName: 'xml',
    headless: true,
    cdata: true,
  });
  return builder.buildObject(obj);
};
